import { Model, Effect } from 'dva';
import { Reducer } from 'redux';
import { list, get, add, update, remove } from '@/services/productAttribute';

export interface ProductAttribute {
  id?: number;
  productAttributeCategoryId?: number;
  name?: string;
  type?: number;
  inputType: number;
  inputList?: string;
}

export interface ProductAttributeModelState {
  attributes: ProductAttribute[];
  attribute?: ProductAttribute;
  currentCategoryId?: number;
  attrType?: number;
}

export interface ProductAttributeModelType extends Model {
  namespace: 'productAttribute';
  state: ProductAttributeModelState;
  effects: {
    query: Effect;
    queryOne: Effect;
    add: Effect;
    update: Effect;
    remove: Effect;
  };
  reducers: {
    refresh: Reducer<ProductAttributeModelState>;
    reloadOne: Reducer<ProductAttributeModelState>;
  };
}

const ProductAttributeModel: ProductAttributeModelType = {
  namespace: 'productAttribute',
  state: {
    attributes: [],
  },

  reducers: {
    refresh(state = { attributes: [] }, action) {
      return {
        ...state,
        attributes: action.attributes,
        currentCategoryId: action.currentCategoryId,
        attrType: action.attrType,
      };
    },
    reloadOne(state, action) {
      return {
        ...state!,
        attribute: action.attribute,
      };
    },
  },

  effects: {
    *query({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({
        type: 'refresh',
        attributes: response,
        currentCategoryId: payload?.productAttributeCategoryId,
        attrType: payload?.type,
      });
    },
    *queryOne({ id }, { call, put }) {
      const response = yield call(get, id);
      yield put({
        type: 'reloadOne',
        attribute: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      yield call(add, payload);
      yield put({
        type: 'query',
      });
      callback();
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(update, payload);
      if (callback) callback();
    },
    *remove({ payload, callback }, { call }) {
      yield call(remove, payload);
      callback();
    },
  },

  subscriptions: {
    // setup({ dispatch, history }) {
    //   history.listen(({ pathname }) => {
    //     if (pathname.startsWith('/product/attribute')) {
    //       dispatch({
    //         type: 'query',
    //       });
    //     }
    //   });
    // },
    setupAdd({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        if (pathname.startsWith('/product/attribute/add')) {
          if (query.categoryId) {
            dispatch({
              type: 'refresh',
              currentCategoryId: Number(query.categoryId),
            });
            dispatch({
              type: 'reloadOne',
              attribute: undefined,
            });
          } else if (query.id) {
            dispatch({
              type: 'queryOne',
              id: query.id,
            });
          }
        }
      });
    },

    setupList({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        if (pathname.startsWith('/product/attribute/list')) {
          dispatch({
            type: 'query',
            payload: {
              productAttributeCategoryId: Number(query.categoryId),
              type: Number(query.type),
            },
          });
        }
      });
    },
  },
};

export default ProductAttributeModel;
